﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;

namespace Wicresoft.UnifyShow.BusinessImpl
{
    public class QueryParameter
    {
        //prevent the sql injeciton?????
        //http://oracle.chinaitlab.com/PLSQL/881289.html
        public string OracleDbColumnName;
        public OracleDbType OracleDbColumnType;
        public int OracleDbColumnSize;
        public object OracleDbColumnValue;
        public QueryOperatorType QueryOperator;
        public PredicateType PredicateType;
    }

    public class JoinCauses
    {
        List<JoinCause> _listJoinCause;

        public JoinCauses()
        {
            _listJoinCause = new List<JoinCause>();
        }

        public void Add(JoinCause newItem)
        {
            foreach (JoinCause item in _listJoinCause)
            {
                if (item.Alia == newItem.TableName) throw new InvalidOperationException("the table have been add to the causes");
                if (newItem.Alia == item.Alia) throw new InvalidOperationException("alia repeat");
                if (newItem.Alia.Equals("z")) throw new InvalidOperationException("the z alia is reserved for the maintable");
            }
            _listJoinCause.Add(newItem);
        }

        public List<JoinCause> Get()
        {
            return _listJoinCause;
        }

    }


    public class JoinCause
    {
        private List<QueryParameter> _listWhereQuery;

        //main table is z alia;
        public string Alia { get; set; }
        public string TableName { get; set; }
        public List<OnQueryParameter> _listOnQuery;
        public JoinType Type { get; set; }

        public JoinCause()
        {
            _listWhereQuery = new List<QueryParameter>();
            _listOnQuery = new List<OnQueryParameter>();
        }

        public void AddQueryParameter(QueryParameter newItem)
        {
            if (newItem.PredicateType != PredicateType.where) throw new InvalidOperationException("only the where predicatetype parameter receive");
            _listWhereQuery.Add(newItem);
        }

        public List<QueryParameter> GetQueryParameter()
        {
            return _listWhereQuery;
        }

        public void AddOnQuery(OnQueryParameter newItem)
        {
            if (!newItem.AliaLeft.Equals(this.Alia) && !newItem.AliaRight.Equals(this.Alia))
                throw new InvalidOperationException("the rightalia or leftalia one should be " + this.Alia);
            _listOnQuery.Add(newItem);
        }

        public List<OnQueryParameter> GetOnQuery()
        {
            return _listOnQuery;
        }

    }

    public class OnQueryParameter
    {
        public string AliaLeft { get; set; }
        public string AliaRight { get; set; }
        public string ColumnNameLeft { get; set; }
        public string ColumnNameRight { get; set; }

        public override string ToString()
        {
            return " " + AliaLeft + "." + ColumnNameLeft + "=" + AliaRight + "." + ColumnNameRight + " ";
        }

    }

    public enum JoinType
    {
        inner = 0,
        left = 1,
        right = 2,
        full = 3
    }


    public enum QueryOperatorType
    {
        none = 0,
        equal = 1,
        notEqual = 2,
        like = 3,
        bigger = 4,
        smaller = 5,
        isNull = 6,
        isNotNull = 7,
        contains = 8,
        biggerNotEqual = 9,
        smallerNotEqual = 10
    }

    public enum PredicateType
    {
        set = 1,
        where = 2,
        insert = 3
    }

    public class OrderbyParameter
    {
        public string orderByCol;
        public OrderByType orderByType;
    }
    public enum OrderByType
    {
        ascending = 1,
        descending = 2
    }
    public enum DBMType
    {
        select = 1,
        update = 2,
        insert = 3,
        delete = 4,
        count=5
    }
}
